package com.becca.cake.service.system.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.becca.cake.dao.system.DataDictionaryDAO;
import com.becca.cake.model.system.DataDictionaryDO;
import com.becca.cake.model.system.MenuTree;
import com.becca.cake.model.system.Node;
import com.becca.cake.service.system.IDataDictionaryService;
import com.becca.cake.util.PMSConstants;

/**
 * 数据字典Service实现类
 * @author hejinguo
 * @version $Id: DataDictionaryServiceImpl.java, v 0.1 2013-12-20 上午01:23:52
 */
@Service
public class DataDictionaryServiceImpl implements IDataDictionaryService {
    @Autowired
    private DataDictionaryDAO dataDictionaryDAO;

    @Override
    @Cacheable(key = "'selectDictionaryTreeMenu'", value = "monmondecakeCache")
    public String selectDictionaryTreeMenu() throws Exception {
        //数据字典顶级ID
        Long parentId = Long.valueOf(PMSConstants.DATA_DISCTIONARY_ROOT);
        //获取数据字典
        List<Node> functions = this.dataDictionaryDAO.selectDictionaryTreeByAdminId(parentId);
        //根据ID查询当前功能
        Node node = this.dataDictionaryDAO.selectDataDictionaryById(parentId);
        //封装菜单树json
        MenuTree tree = new MenuTree();
        tree.getTree(functions, node, false);
        return tree.modifyStr(tree.returnStr.toString());
    }

    @Override
    public DataDictionaryDO selectDataDictionaryMsgById(String id) throws Exception {
        return this.dataDictionaryDAO.selectDataDictionaryMsgById(id);
    }

    @Override
    @CacheEvict(key = "'selectDictionaryTreeMenu'", value = "monmondecakeCache")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insetDataDictionary(DataDictionaryDO dataDictionaryDO) throws Exception {
        this.dataDictionaryDAO.insetDataDictionary(dataDictionaryDO);
    }

    @Override
    @CacheEvict(key = "'selectDictionaryTreeMenu'", value = "monmondecakeCache")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateDataDictionaryById(DataDictionaryDO dataDictionaryDO) throws Exception {
        this.dataDictionaryDAO.updateDataDictionaryById(dataDictionaryDO);
    }

    @Override
    @Cacheable(key = "'selectDictionaryTreeMenu'", value = "monmondecakeCache")
    public List<DataDictionaryDO> selectDataDictionary(String dictionaryType, String useable)
                                                                                             throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("dictionaryType", dictionaryType);
        paramMap.put("useable", useable);
        return this.dataDictionaryDAO.selectDataDictionary(paramMap);
    }
}
